<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="上级类型" prop="parentName">
        <el-input v-model="parentName" disabled />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入分类名称" />
      </el-form-item>
      <el-form-item label="分类编码" prop="code">
        <el-input v-model="formItem.code" placeholder="请输入分类编码" />
      </el-form-item>
      <el-form-item label="计量单位" prop="measureUnit">
        <el-input v-model="formItem.measureUnit" placeholder="请输入计量单位" />
      </el-form-item>
      <el-form-item label="分类顺序" prop="sort">
        <el-input v-model="formItem.sort" placeholder="请输入分类顺序" />
      </el-form-item>
      <el-form-item label="提示：">
        <span>新增分类如果是父分类唯一的子分类，父分类下的资产将被迁移到该新增的子分类下</span>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create
} from '@neu/dcam-ui/api/assetType'

// 定义一个表单原始项
const formItem = {
  name: '',
  code: '',
  measureUnit: '',
  sort: '',
  parentId: null
}
export default {
  name: 'FixedAssetTypeEditForm',
  props: {
    // 根据id可判断是新建还是更新
    parentId: {
      required: false,
      type: Number,
      default: null
    },
    parentName: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '分类名称不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 50) {
              callback(new Error('最多可输入50个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
        ],
        code: [{
          required: true,
          message: '分类编码不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 50) {
              callback(new Error('最多可输入50个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
        ],
        measureUnit: [{
          required: true,
          message: '计量单位不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 10) {
              callback(new Error('最多可输入10个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
        ],
        sort: [{
          required: true,
          message: '分类不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (/^[0-9]*$/.test(value) === false) {
              callback(new Error('请输入正确的数字'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
        ]
      },
      modal: true,
      /** 资产类型树 */
      typeOptions: []
    }
  },
  methods: {
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      // const updated = this.parentId !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.parentId = this.parentId !== -1 ? this.parentId : null
            resolve(create(this.formItem))
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
